package my.sorter.config;

import my.sorter.security.StaffRealm;
import my.sorter.security.test.TestRedisCacheManager;
import my.sorter.security.test.TestRedisSession;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author zyf10
 */
//@Configuration
//@Cacheable
//@ComponentScan
public class ShiroConfig {

    @Autowired
    private JedisConnectionFactory jedisConnectionFactory;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Bean(name = "shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //拦截器
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        // 配置不会被拦截的链接 顺序判断
        filterChainDefinitionMap.put("/login", "anon");
//        filterChainDefinitionMap.put("/response", "anon");
        filterChainDefinitionMap.put("/register", "anon");
//        filterChainDefinitionMap.put("/security/**", "anon");
//        filterChainDefinitionMap.put("/**", "anon");
        filterChainDefinitionMap.put("/**", "authc");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        shiroFilterFactoryBean.setLoginUrl("/response");
        return shiroFilterFactoryBean;
    }

    @Bean
    public StaffRealm staffRealm() {
        return new StaffRealm();
    }

    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(staffRealm());
        securityManager.setCacheManager(shiroRedisCacheManager());
        securityManager.setSessionManager(sessionManager());
        return securityManager;
    }

    @Bean
    public TestRedisCacheManager shiroRedisCacheManager() {
        return new TestRedisCacheManager(jedisConnectionFactory, redisTemplate);
    }

    @Bean
    public TestRedisSession sessionDao() {
        TestRedisSession sessionDao = new TestRedisSession();
        sessionDao.setCacheManager(shiroRedisCacheManager());
        return sessionDao;
    }

    @Bean
    public DefaultWebSessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionDAO(sessionDao());
        return sessionManager;
    }

    //    @Bean
//    public DefaultWebSessionManager sessionManager(){
//        DefaultWebSessionManager sessionManager = new RedisSessionManager();
//        sessionManager.setSessionDAO(sessionDao());
//        sessionManager.setSessionIdUrlRewritingEnabled(false);
//        //设置session过期时间为1小时(单位：毫秒)，默认为30分钟
//        sessionManager.setGlobalSessionTimeout(30* 60 * 1000);
//        sessionManager.setDeleteInvalidSessions(true);
//        sessionManager.setCacheManager(shiroRedisCacheManager());
//        sessionManager.setSessionValidationSchedulerEnabled(false);
//        Cookie sessionIdCookie = sessionManager.getSessionIdCookie();
//        // 这里可以扩大cookie的作用域范围
//        // sessionIdCookie.setDomain("");
//        sessionIdCookie.setPath("/");
//        // 这里可以自定义sessionId在cookie中的键
//        //sessionIdCookie.setName("csid");
//        sessionManager.setSessionIdCookieEnabled(true);
//        sessionManager.setSessionIdUrlRewritingEnabled(false);
//        return sessionManager;
//    }
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor
                = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
}
